import api from "@/sdk/api/api.js"
export default {
	state:{
		statusList:{
			// 状态:0=待付款,1=待发货,2=待收货,3=待评价,4=已完成,5=申请退货/退款,6=已退回,-1=已取消,7=售后,
			"0":{
				name:"待付款",
				buttons:[
					"cancellation",//取消
					"payment",//支付
				]
			},
			"1":{
				name:"待发货",
				buttons:[
					"refund",//退款
					"confirm_receipt",//确认收货
				]
			},
			"2":{
				name:"待收货",
				buttons:[
					"refund",//退款
					"confirm_receipt",//确认收货
					"wuliu",//物流
				]
			},
			"4":{
				name:"待评价",
				buttons:[
					"comment",//评论
					// "confirm_receipt",//确认收货
				]
			},
			"5":{
				name:"已完成",
				buttons:[
					// "comment",//评论
					// "confirm_receipt",//确认收货
					"del",//删除
					"sales",//售后
				]
			},
			"6":{
				name:"申请退货",
				buttons:[
					// "comment",//评论
					// "confirm_receipt",//确认收货
					// "del",//删除
				]
			},
			"7":{
				name:"已退回",
				buttons:[
					// "comment",//评论
					// "confirm_receipt",//确认收货
					"del",//删除
				]
			},
			"8":{
				name:"退款失败",
				buttons:[
					// "comment",//评论
					"confirm_receipt",//确认收货
					// "del",//删除
				]
			},
			"-1":{
				name:"已取消",
				buttons:[
					// "comment",//评论
					// "confirm_receipt",//确认收货
					// "del",//删除
				]
			}
		},
		tabList:[
			{
				id:0,
				name:"待付款",
				icon:"../../static/user/order1.png",
				corner:0,
				index:1,
			},
			{
				id:1,
				name:"待发货",
				icon:"../../static/user/order2.png",
				corner:0,
				index:2,
			},
			{
				id:2,
				name:"待收货",
				icon:"../../static/user/order3.png",
				corner:0,
				index:3
			},
			{
				id:5,
				name:"已完成",
				icon:"../../static/user/order4.png",
				corner:0,
				index:5
			},
			{
				id:4,
				name:"待评价",
				icon:"../../static/user/order5.png",
				corner:0,
				index:4
			},
		],
		orderList:[
			{
				id: 'all',
				name: "全部",
				page:1,
				data: [
				]
			},
			{
				id: '0',
				name: "待付款",
				page:1,
				data: []
			},
			{
				id: '1',
				name: "待发货",
				page:1,
				data: []
			},
			{
				id: '2',
				name: "待收货",
				page:1,
				data: []
			},
			{
				id: '4',
				name: "待评价",
				page:1,
				data: []
			},
			{
				id: '5',
				name: "已完成",
				page:1,
				data: [
				]
			},
			{
				id: '6',
				name: "退款/售后",
				page:1,
				data: [
				]
			}
		]
	},
	mutations:{
		//修改用户信息
		changeOrderStatus(state,tabList){
			console.log(tabList);
			state.tabList = tabList
		},
		changeOrderList(state,orderList){
			console.log(orderList);
			state.orderList = orderList;
		}
	},
	actions:{
		//获取用户信息
		getOrderStatus(context){
			// console.log("进入order");
			let orderStatusList = context.state.tabList
			api.getOrderStatus().then(res=>{
				if(res){
					orderStatusList.forEach(item=>{
						if(res[item.id]){
							item.corner = res[item.id]
						}else{
							item.corner = 0
						}
						
					})
					// console.log(orderStatusList);
					context.commit("changeOrderStatus",orderStatusList);
				}
			})
		},
		//获取订单列表
		async getOrderList(context,tabCode){
			// console.log(tabCode);
			let orderList = context.state.orderList;
			if((tabCode==""||tabCode==undefined)&&tabCode!=0){
				for(let a in orderList){
					orderList[a].page = 1;
					orderList[a].data = [];
				}
				context.commit("changeOrderList",orderList)
				for(let b in orderList){
					let res = await api.getOrderList({
						state:orderList[b].id,
						page:1
					})
					if(res.data.length>0){
						orderList[b].page = 2;
						orderList[b].data = res.data;
					}else{
						orderList[b].page = 0;
					}
				}
			}else{
				console.log(orderList[tabCode]);
				if(orderList[tabCode].page==0){
					return
				}
				let res = await api.getOrderList({state:orderList[tabCode].id,page:orderList[tabCode].page});
				if(res.data.length>0){
					orderList[tabCode].data.push(...res.data);
					orderList[tabCode].page+=1
				}else{
					orderList[tabCode].page = 0
				}
			}
			context.commit("changeOrderList",orderList)
		},
		clearTabList(context){
			let tabList = context.state.tabList;
			tabList.forEach(item=>{
				item.corner = 0
			})
			// console.log(tabList);
			context.commit("changeOrderStatus",tabList)
		}
	}
}